package com.wimcorp.magic.behaviour.attack;

import javax.persistence.Entity;

import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.players.Player;

@Entity
public class NotFirstStrike extends AttackBehaviour {

	private static NotFirstStrike instance = new NotFirstStrike();

	protected NotFirstStrike() {

	}

	public NotFirstStrike(Creature creature, AttackBehaviour attackBehaviour) {
		super(creature, attackBehaviour);
	}

	public NotFirstStrike(Creature creature) {
		super(creature, null);
	}

	public static NotFirstStrike instance() {
		return instance;
	}

	protected void attack(Creature defender) {
		if (defender.hasBehaviour(FirstStrike.instance())) {
			defender.counter(getCreature());
			if(getCreature().isAlive()){
				getAttackBehaviour().attack(defender);
			}
		}else if (defender.hasBehaviour(DoubleStrike.instance())) {
			int power = defender.getActualPower();
			defender.counter(getCreature());
			defender.setActualPower(power);
			if(getCreature().isAlive()){
				defender.counter(getCreature());
				getAttackBehaviour().attack(defender);
			}
		}else{
			getAttackBehaviour().attack(defender);
			defender.counter(getCreature());
		}
	}
	
	@Override
	public void counter(Creature attacker) {
		getAttackBehaviour().counter(attacker);
	}

	protected void attack(Player defender) {
		//defender.subtractLife(getCreature().getActualPower());
		//getCreature().setActualPower(0);
		getAttackBehaviour().attack(defender);
	}

	public int priority() {
		return AttackType.FIRST_STRIKE.ordinal();
	}

	public boolean equals(Object o) {
		return o instanceof NotFirstStrike;
	}
}
